package com.common.www.controller.auth;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.auth.AuthRole;
import com.common.www.bean.entity.auth.AuthUser;
import com.common.www.service.auth.RoleService;
import com.common.www.service.auth.UserService;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin()
@Api(tags = "MGT 用户角色相关接口")
@RequestMapping(value = {"/mgt/auth/role/"})
public class MGTRoleController extends DefaultCrudController<AuthRole, Long> {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    @Override
    protected IBaseService getService() {
        return roleService;
    }

    @ApiOperation("添加用户的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        AuthRole authRole = JSON.parseObject(json, AuthRole.class);
        return super.save(authRole);
    }

    @ApiOperation("得到用户的角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/getRoleListByUser", method = RequestMethod.POST)
    public Result getRoleListByUser(String userId) {
        AuthUser authUser = userService.getById(userId);
        if (authUser != null) {
            return R.ok(authUser.getAuthRoles());
        } else {
            return R.ok();
        }
    }


}
